package cn.yhbk.web.common.service.impl;

import cn.hutool.json.*;
import cn.yhbk.web.common.service.OCRCService;
import cn.yhbk.web.modules.manager.Pojo.DTO.GpaCheck.Words;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@Service
@Slf4j
public class OCRServiceImpl implements OCRCService {

    private final RestTemplate restTemplate;


    public OCRServiceImpl(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }


    public List<Words> performOCR(@RequestBody List<String> imgUrls) {
        // 构建向 Python 服务的请求
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject requestBody = new JSONObject();
        requestBody.put("img_paths", imgUrls);

        HttpEntity<String> request = new HttpEntity<>(requestBody.toString(), headers);

        // 发送请求到 Flask 服务
        String flaskServiceUrl = "http://localhost:5000/ocr";
        ResponseEntity<String> response = restTemplate.postForEntity(flaskServiceUrl, request, String.class);

//        String str = "[\n" +
//                "\t{\n" +
//                "\t\t\"img_path\": \"http://111.230.113.146:9000/bursary/2024-02-04/image/1707021369887.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=youhebuke%2F20240204%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240204T043610Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=a657ac4de17b99edc98000842258965498069c864e1d39a6053cf83589646e9e\",\n" +
//                "\t\t\"result\": [\n" +
//                "\t\t\t\"获奖\",\n" +
//                "\t\t\t\"证书\",\n" +
//                "\t\t\t\"四川轻化工大学作品《基于云计算平台构建教学管理平\",\n" +
//                "\t\t\t\"台》在2023年四川省大学生计算机设计大赛暨第16届中国大\",\n" +
//                "\t\t\t\"学生计算机设计大赛四川省级赛（本科组）中荣获\",\n" +
//                "\t\t\t\"二等奖\",\n" +
//                "\t\t\t\"作者：文定、张航、李琳\",\n" +
//                "\t\t\t\"指导教师：陈超\",\n" +
//                "\t\t\t\"作品大类：软件应用与开发\",\n" +
//                "\t\t\t\"作品小类：Web应用与开发\",\n" +
//                "\t\t\t\"证书编号：SCJSJ202301010143\",\n" +
//                "\t\t\t\"算机设计\",\n" +
//                "\t\t\t\"四川省\",\n" +
//                "\t\t\t\"学生计算机设计大赛\",\n" +
//                "\t\t\t\"大赛\",\n" +
//                "\t\t\t\"高\",\n" +
//                "\t\t\t\"组织委员会\",\n" +
//                "\t\t\t\"四川香级客\",\n" +
//                "\t\t\t\"计算概基础教\",\n" +
//                "\t\t\t\"西川省级赛\",\n" +
//                "\t\t\t\"组织委员会\",\n" +
//                "\t\t\t\"2023年5月\"\n" +
//                "\t\t]\n" +
//                "\t}\n" +
//                "]";

        return com.alibaba.fastjson.JSON.parseArray(response.getBody(), Words.class);
    }
}
